package model;

import init.DBManagement;

import java.util.ArrayList;
import java.util.Map;

import model.beans.UnidadBean;

public class Units {
	
	public static ArrayList list(int int_planEstudio, String str_academy, String str_subject){
		ArrayList arrL_retVal = new ArrayList();
		UnidadBean bn_unit;

		String str_list =
			" SELECT  PLU_TECH_KEY,                                     " +
			"       NUMERO ,  " +
			"        DESCRIPCION                 " +
			" FROM PLAUNIR                            " + 
			" WHERE  PLAN_TK = '"+int_planEstudio +"' " ;
		
		DBManagement database = new DBManagement();
		database.executeQuery( str_list );

		for( int int_i = 0; int_i < database.getNumRows(); int_i++ ){
			bn_unit = new UnidadBean();
			bn_unit.setInt_unitId(Integer.parseInt( database.getItem(int_i, 0) ));
			bn_unit.setInt_unitNumber(Integer.parseInt( database.getItem(int_i, 1) ));
			bn_unit.setStr_descripcion(database.getItem(int_i, 2));
			bn_unit.setAcademy(str_academy);
			bn_unit.setSubject(str_subject);
			
			arrL_retVal.add(bn_unit);
		}
		database = null;
		return arrL_retVal;
	}
	
	public static UnidadBean instance( int int_unitKey, String str_academyCode, String str_subjectCode ){
		UnidadBean unidad_retVal = null;
		String str_list = 
			" SELECT  PLU_TECH_KEY,                                     " +
			"       NUMERO ,  " +
			"        DESCRIPCION,                 " +
			"        PLAN_TK                          " +
			" FROM PLAUNIR                            " + 
			" WHERE  PLU_TECH_KEY = '"+int_unitKey +"' " ;
		DBManagement database = new DBManagement();
	    database.executeQuery( str_list );
	  
	    if( database.getNumRows() == 1 ){
	    	unidad_retVal = new UnidadBean();
	    	unidad_retVal.setInt_unitId(Integer.parseInt(database.getItem(0, 0)));
	    	unidad_retVal.setInt_unitNumber(Integer.parseInt(database.getItem(0, 1)));
	    	unidad_retVal.setStr_descripcion(database.getItem(0, 2));
	    	unidad_retVal.setInt_planEstudio((Integer.parseInt(database.getItem(0, 3))));	   
	    	unidad_retVal.setAcademy(str_academyCode);
	    	unidad_retVal.setSubject(str_subjectCode);
	    }
	    database = null;
		return unidad_retVal;
	}
	
	public static int add( Map map_data, int int_planEstudio){
		int int_add;
		Integer int_unitNumber = (Integer)map_data.get("int_unitNumber");
		String str_descripcion = (String)map_data.get("str_descripcion");
		String str_add =
            " INSERT INTO PLAUNIR ( "+
            "        NUMERO, "+
            "        DESCRIPCION, "+
            "        PLAN_TK "+
            " ) VALUES ( "+
            "        '"+int_unitNumber+"', "+
            "        '"+str_descripcion+"', "+
            "        '"+int_planEstudio+"' "+
            " ) ";
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_add );
        int_add = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_add;
	}
	
	public static int edit(Map map_data){
		int int_edit;
		Integer int_unitNumber = (Integer)map_data.get("int_unitNumber");
		String str_descripcion = (String)map_data.get("str_descripcion");
		Integer int_unitKey = (Integer)map_data.get("int_unitKey");
		String str_edit =
			 " UPDATE PLAUNIR "+
	            " SET    NUMERO = '"+int_unitNumber+"', "+
	            "        DESCRIPCION =  '"+str_descripcion+"' "+	         
	            " where  PLU_TECH_KEY = "+int_unitKey;
		
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_edit );
        int_edit = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_edit;
	}
	
	public static int delete( int int_unitKey ){
		int int_delete;
		String str_delete =
            " delete from PLAUNIR "+
            " where  PLU_TECH_KEY = "+int_unitKey;
		DBManagement database = new DBManagement();
        boolean bol_insertStatus = database.executeQuery( str_delete );
        int_delete = bol_insertStatus ? 1 : 0;
        database = null;
	    return int_delete;
	}
}
